﻿using Knight.Web.Infrastructure.Menus;
using Knight.Web.Infrastructure.Mvc;

using Knight.Models.Entity;
using Knight.Models.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApp.Areas.Manage.Controllers.Filters;
using Knlght.Domain;

namespace WebApp.Areas.Manage.Controllers
{
    public class LogController : BaseAdminController
    {
        [MallAdminMenu("System_Log")]
        [Permission("System_Log")]
        public ActionResult Index()
        {
            //日志枚举
            ViewBag.DbLogType = Knight.Common.Helper.EnumberHelper.EnumToList<DbLogType>();
            return View();
        }

        #region AngularJs
        [HttpPost]
        public JsonResult Paging(PagingParameter parameter)
        {
            var query = new QueryRequest<LogInfo>();
            if (parameter.Query.CreateCriterionByString(nameof(LogInfo.Description)) != null)
            {
                var description = parameter.Query.CreateCriterionByString(nameof(LogInfo.Description)).Value.ToString();
                query.Add(o => o.Description.Contains(description));
            }
            if (parameter.Query.CreateCriterionByString(nameof(LogInfo.Username)) != null)
            {
                var nickName = parameter.Query.CreateCriterionByString(nameof(LogInfo.Username)).Value.ToString();
                query.Add(o => o.Username.Contains(nickName));
            }
            if (parameter.Query.CreateCriterionByString(nameof(LogInfo.Account)) != null)
            {
                var account = parameter.Query.CreateCriterionByString(nameof(LogInfo.Account)).Value.ToString();
                query.Add(o => o.Account.Contains(account));
            }
            if (parameter.Query.CreateCriterionByBoolean(nameof(LogInfo.Result)) != null)
            {
                var result = Convert.ToBoolean(parameter.Query.CreateCriterionByBoolean(nameof(LogInfo.Result)).Value);
                query.Add(o => o.Result == result);
            }
            query.AddOrder(nameof(LogInfo.AddTime), true);     
            var pagingRequest = new PagingRequest<LogInfo>
            {
                PageIndex = parameter.PageIndex,
                PageSize = parameter.PageSize,
                Query = query
            };
            var service = getDefaultService();
            var rsp = service.Paging(pagingRequest);
            return EJPagingJson(rsp);
        }
        [HttpPost]
        public JsonResult Remove(int id)
       {
            var service = getDefaultService();
            var rsp = service.Remove<LogInfo>(o => o.Id == id);
            return EJActionJson(rsp);
        }
        [HttpPost]
        public JsonResult Removes(int[] ids)
        {
            var service = getDefaultService();
            var rsp = service.Remove<LogInfo>(o => ids.Contains(o.Id));
            return EJActionJson(rsp);
        }
        #endregion
    }
}